package net.hands.common.mail;

import java.util.Properties;

import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;

import com.sun.mail.pop3.POP3SSLStore;
/**
 * 
 * 说明:Pop3加密服务
  * <br>
 * <p>这是一个开源的软件，请在LGPLv3下合法使用、修改或重新发布。</p>
 * @author 陈侃(cknet@126.com, 13910090885)
 *         http://code.google.com/p/simpleframework/
 *         http://www.simpleframework.net
 */
public class Pop3SslServer extends Pop3Server {
   /**
    * pop3加密端口
    */
	protected static final String MAIL_POP3_SOCKET_FACTORY_PORT = "mail.pop3.socketFactory.port";
	/**
	 * pop3加密类
	 */
	protected static final String MAIL_POP3_SOCKET_FACTORY_CLASS = "mail.pop3.socketFactory.class";
	/**
	 * pop3加密回退
	 */
	protected static final String MAIL_POP3_SOCKET_FACTORY_FALLBACK = "mail.pop3.socketFactory.fallback";
	/**
	 * 默认端口
	 */
	protected static final int DEFAULT_SSL_PORT = 995;
   /**
    * 用户名
    */
	protected final String username;
	/**
	 * 密码
	 */
	protected final String password;
   /**
    * 
   * Constructor Method  
   * @update 2012-9-21 上午11:13:08  
   * @param host 主机
   * @param username 用户名
   * @param password 密码
    */
	public Pop3SslServer(final String host, final String username, final String password) {
		this(host, DEFAULT_SSL_PORT, username, password);
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-21 上午11:13:13  
   * @param host 主机
   * @param port 端口
   * @param username 用户名
   * @param password 密码
    */
	public Pop3SslServer(final String host, final int port, final String username,
			final String password) {
		super(host, port, username, password);
		this.username = username;
		this.password = password;
	}
   /**
    * 
   (non-Javadoc)  
   * @see net.hands.common.mail.Pop3Server#createSessionProperties()  
   * @update 2012-9-21 上午11:15:26
    */
	@Override
	protected Properties createSessionProperties() {
		final Properties props = new Properties();
		props.setProperty(MAIL_POP3_PORT, String.valueOf(port));
		props.setProperty(MAIL_POP3_SOCKET_FACTORY_PORT, String.valueOf(port));
		props.setProperty(MAIL_POP3_SOCKET_FACTORY_CLASS, "javax.net.ssl.SSLSocketFactory");
		props.setProperty(MAIL_POP3_SOCKET_FACTORY_FALLBACK, "false");
		return props;
	}
   /**
    * 
   (non-Javadoc)  
   * @see net.hands.common.mail.Pop3Server#getStore(javax.mail.Session)  
   * @update 2012-9-21 上午11:15:38
    */
	@Override
	protected Store getStore(final Session session) throws NoSuchProviderException {
		final URLName url = new URLName(PROTOCOL_POP3, host, port, "", username, password);
		return new POP3SSLStore(session, url);
	}
}